<cfset today = now()>
<cfset mytime = timeformat(now(), "hh:mm:ss tt")>


<cfif isdefined("FORM.ProdID")>
  <CFQUERY NAME="getprod" DATASOURCE="#dsn#">
select *
from tbltransactions
where ProdID = '#ProdID#' and ReceiptNum = #session.ReceiptNum#
</CFQUERY>
  <cfoutput query="getprod" maxrows="1">
    <cfquery datasource="#dsn#" maxrows="1">
delete
from tbltransactions
where ProdID = '#ProdID#' and ReceiptNum = #session.ReceiptNum# and KeyID = #getprod.KeyID#
</cfquery>

<!---add back to inventory......................................................
--->

<CFQUERY NAME="getprod" DATASOURCE="#dsn#">
select *
from tblproducts
where ProdID = '#ProdID#'
</CFQUERY>


<cfset myqty = #getprod.Qty# + 1>



<cfquery datasource="#dsn#">
update tblproducts
set Qty = #myqty#
where ProdID = '#ProdID#'
</cfquery>



<cfquery datasource="#dsn#">
INSERT INTO tblinventory(ReceiptNum, ProdID, Price, DateIn, DODAAC, Username, Terminal, Qty, TransType, Cost)
VALUES(#session.ReceiptNum#, 
'#ProdID#', 
#getprod.Price#, 
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#today#">,
<cfif isdefined("session.DODAAC")>#session.DODAAC#<cfelse>null</cfif>,
'#session.username#',
#session.terminal#,
#getprod.Qty#,
'VOID',
<cfif #getprod.Cost# lte 0>0<cfelse>#getprod.Cost#</cfif>)
</cfquery>




  </cfoutput><cfoutput>
    <CFQUERY NAME="Recordset1" DATASOURCE="#dsn#">
select sum(Price) as total, sum(TaxAmount) as taxtotal
from tbltransactions
where ReceiptNum = #session.ReceiptNum#
</CFQUERY>
    <!---update main invoice with total amount-------------------->
    
	<CFIF #Recordset1.total# LTE 0>
    <CFSET #Recordset1.total# = 0></CFIF>
    
    <CFIF #Recordset1.taxtotal# LTE 0>
    <CFSET Recordset1.taxtotal = 0></CFIF>
	



	<cfset mytaxtotal = #Recordset1.total# + #Recordset1.taxtotal#>
    
    
    
    
    
    
    
    
    
    
    <cfquery datasource="#dsn#">
update tblorders
set Amount = #mytaxtotal#
where ReceiptNum = #session.ReceiptNum# 
</cfquery>
    <cfset session.TransType = "SALE">
    <CFQUERY DATASOURCE="#dsn#">
INSERT INTO tbltranslog (Code, ReceiptNum, TransDate, TransTime, username, ProdID, Amount)
VALUES (
'V',
#session.ReceiptNum#,
<cfqueryparam cfsqltype="cf_sql_date" value="#today#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#mytime#">,
'#session.username#',
'#ProdID#',
#mytaxtotal#

)
</CFQUERY>
  </cfoutput>
  <cflocation url="products.cfm">
</cfif>
